package own.stu.jobgib.playown.alg.solution;


import org.apache.commons.lang3.tuple.Pair;
import own.stu.jobgib.playown.alg.leetcode.editor.cn.TreeNode;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    public static void main(String[] args) {


        TreeNode node = toTree(new Integer[]{10, 5, -3, 3, 2, null, 11, 3, -2, null, 1});


    }

    public static void printTree(TreeNode node) {
        /*if (node == null) return;
        Deque<TreeNode> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode tmp = queue.removeFirst();
                list.add(tmp.val)
            }

        }*/
    }

    public static TreeNode toTree(Integer[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }

        int n = nums.length;
        TreeNode root = new TreeNode(nums[0]);

        Deque<Pair<TreeNode, Integer>> queue = new LinkedList<>();
        queue.add(Pair.of(root, 0));
        while (!queue.isEmpty()) {
            Pair<TreeNode, Integer> pair = queue.removeFirst();
            TreeNode node = pair.getLeft();
            if (node == null) continue;
            int idx = pair.getRight();
            int leftChidIdx = idx * 2 + 1;
            int rightChidIdx = idx * 2 + 2;
            if (leftChidIdx < n && nums[leftChidIdx] != null) {

                TreeNode left = new TreeNode(nums[leftChidIdx]);
                node.left = left;
                queue.add(Pair.of(left, leftChidIdx));
            }
            if (rightChidIdx < n && nums[rightChidIdx] != null) {
                TreeNode right = new TreeNode(nums[rightChidIdx]);
                node.right = right;
                queue.add(Pair.of(right, rightChidIdx));
            }
        }

        return root;
    }

    public static void print_I(boolean[][] a, int flag) {
        int n = a.length; //, m = a[0].length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (flag == 1) {
                    if (j <= i) {
                        System.out.print(a[i][j] + ", ");
                    } else {
                        System.out.print(_ + ", ");
                    }
                } else if (flag == 2) {
                    if (j >= i) {
                        System.out.print(a[i][j] + ", ");
                    } else {
                        System.out.print(_ + ", ");
                    }
                }
            }
            System.out.println();
        }

        System.out.println();
    }

    public static void print_II(boolean[][] a, int flag) {
        int n = a.length; //, m = a[0].length;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (flag == 1) {
                    if (j <= i) {
                        System.out.print(a[i][j] + ", ");
                    } else {
                        System.out.print(_ + ", ");
                    }
                } else if (flag == 2) {
                    if (j >= i) {
                        System.out.print(a[i][j] + ", ");
                    } else {
                        System.out.print(_ + ", ");
                    }
                }
            }
            System.out.println();
        }

        System.out.println();
    }

    static String _ = "____";

    public static void print(boolean[][] aa) {
        for (boolean[] a : aa) {
            for (boolean _ : a) {
                System.out.print(_ + ", ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void print(Object[][] aa) {
        for (Object[] a : aa) {
            for (Object _ : a) {
                System.out.print(_ + ", ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void print(int[][] aa) {
        for (int[] a : aa) {
            for (Object _ : a) {
                System.out.print(_ + ", ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static int[] toIntArray(String str) {
        str = str.substring(1, str.length() - 1);
        return Arrays.asList(str.split(",")).stream()
                .mapToInt(v -> Integer.valueOf(v))
                .toArray();
    }

    public static int[][] toIntArrays(String str) {
        str = str.substring(1, str.length() - 1);
        List<String> strs = match(str);
        List<int[]> res = new ArrayList<>();
        for (String s : strs) {
            res.add(toIntArray(s));
        }
        int[][] arr = new int[res.size()][];
        return res.toArray(arr);
    }

    private static List<String> match(String str) {
        List<String> list = new ArrayList<>();

        Matcher matcher = compile.matcher(str);
        while (matcher.find()) {
            list.add(matcher.group());
        }
        return list;
    }

    static Pattern compile = Pattern.compile("\\[-?\\d+\\,-?\\d+\\]");
}
